local U = require "packages.utility.utility"

local cairen = fk.CreateSkill{
  name = "xiaobai__cairen"
}

Fk:loadTranslationTable{
  ["xiaobai__cairen"] = "豺刃",
  [":xiaobai__cairen"] = "每回合限一次，你可以弃置一张非基本牌，或重铸两张基本牌以视为使用一张【杀】，"..
    "若之：花色相同，你获得一张【影】；均为【影】，此【杀】无距离限制。",
    
  ["#xiaobai__cairen"] = "豺刃：你可以弃置一张非基本牌，或重铸两张基本牌以视为使用一张【杀】",
}

cairen:addEffect("viewas", {
  prompt = "#xiaobai__cairen",
  anim_type = "offensive",
  pattern = "slash", 
  card_filter = function (self, player, to_select, selected)
    if not table.contains(player:getCardIds("h"), to_select) then return end
    if #selected == 0 then
      return true
    elseif #selected == 1 then
      if Fk:getCardById(selected[1]).type ~= Card.TypeBasic then return
      else
        return Fk:getCardById(to_select).type == Card.TypeBasic
      end
    end
  end,
  max_turn_use_time = 1,
  view_as = function (self, player, cards)
    local slash = Fk:cloneCard("slash")
    slash.skillName = cairen.name
    if #cards == 1 and Fk:getCardById(cards[1]).type ~= Card.TypeBasic or #cards == 2 then
      slash:addFakeSubcards(cards)
      return slash
    end
  end,
  before_use = function (self, player, use)
    local room = player.room
    if #use.card.fake_subcards == 1 then
      room:throwCard(use.card.fake_subcards, self.name, player)
    else
      room:recastCard(use.card.fake_subcards, player, self.name)
    end
  end,
  after_use = function (self, player, use)
    if #use.card.fake_subcards == 1 or (Fk:getCardById(use.card.fake_subcards[1]).type == Fk:getCardById(use.card.fake_subcards[2]).type) then
      player.room:obtainCard(player, U.getShade(player.room, 1), true, fk.ReasonPrey, player, cairen.name)
    end
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(cairen.name) == 0
  end,
  enabled_at_response = function (self, player, response)
    return player:usedSkillTimes(cairen.name) == 0 and not response
  end,
})

cairen:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    if table.contains(card.skillNames, cairen.name) then
      for _, c in ipairs(card.fake_subcards) do
        if Fk:getCardById(c).trueName ~= "shade" then
          return
        end
      end
      return true
    end
  end
})

return cairen